<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>DataMapper Models : DataMapper User Guide</title>

<style type='text/css' media='all'>@import url('../css/userguide.css');</style>
<link rel='stylesheet' type='text/css' media='all' href='../css/userguide.css' />

<meta http-equiv='expires' content='-1' />
<meta http-equiv= 'pragma' content='no-cache' />
<meta name='robots' content='all' />

</head>

<body>

<!-- START NAVIGATION -->
<div id="nav"><div id="nav_inner"></div></div>
<div id="nav2"><a name="top">&nbsp;</a><a id="nav_toggle" href="#"><img src="../images/nav_toggle_darker.jpg" width="154" height="43" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div>
<div id="masthead">
<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
<tr>
<td><h1>DataMapper</h1></td>
<td id="breadcrumb_right"><a href="toc.html">Table of Contents Page</a></td>
</tr>
</table>
</div>
<!-- END NAVIGATION -->

<!-- START BREADCRUMB -->
<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
<tr>
<td id="breadcrumb">
<a href="http://stensi.com/">DataMapper Home</a> &nbsp;&#8250;&nbsp;
<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
DataMapper Models
</td>
</tr>

</table>
<!-- END BREADCRUMB -->

<br clear="all" />


<!-- START CONTENT -->
<div id="content">


<h1>DataMapper Models</h1>

<p>In order for DataMapper to map your Database tables into objects, you first need to create a DataMapper model for each table.  These models will extend DataMapper in order to gain the wonderful functionality of tables as objects.</p>

<p>If you are unfamiliar with the basic workings of models, I recommend you read <a href="http://codeigniter.com/user_guide/general/models.html">Models</a> in the CodeIgniter User Guide.</p>

<h2>Basic Template</h2>
<p>Below is a basic template you can use to create DataMapper models.</p>
<ul>
	<li><var>Name</var> - Replace this value with the name of your object.  For example: <strong>User</strong></li>
	<li><samp>DataMapper</samp> - Specifying DataMapper in these places is what makes your model a DataMapper model.</li>
</ul>
<code>
class <var>Name</var> extends <samp>DataMapper</samp> {<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;function <var>Name</var>()<br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parent::<samp>DataMapper</samp>();<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
}<br />
<br />
/* End of file <var>name</var>.php */<br />
/* Location: ./application/models/<var>name</var>.php */
</code>
	

<h2>Rules</h2>

<p>DataMapper models must be named the singular version of the object name, with an uppercase first letter. So for a user object, the DataMapper model would be named <b>User</b>.  The model should have a corresponding table in the database named as the lowercase, pluralised version of the object name.  So for a DataMapper model named <b>User</b>, the table would be named <b>users</b>.  For a DataMapper model named <b>Country</b>, the table would be named <b>countries</b>.</p>

<p>In most cases, the difference between the singular and plural version of an object name is just a matter of adding the letter <b>s</b> on the end.  For example:</p>

<table cellpadding="0" cellspacing="1" border="0" class="tableborder">
<tr>
	<th>Model name</th>
	<th>Table name</th>

</tr>
<tr>
	<td class="td">Author</td>
	<td class="td">authors</td>
</tr>
<tr>
	<td class="td">Book</td>
	<td class="td">books</td>
</tr>
<tr>
	<td class="td">Genre</td>
	<td class="td">genres</td>
</tr>
</table>

<p>However, some object names have completely different wording between the singular and plural. For example:</p>

<table cellpadding="0" cellspacing="1" border="0" class="tableborder">
<tr>
	<th>Model name</th>
	<th>Table name</th>

</tr>
<tr>
	<td class="td">Country</td>
	<td class="td">countries</td>
</tr>
<tr>
	<td class="td">Person</td>
	<td class="td">People</td>
</tr>
</table>

<p>In this case, you will need to specify the table name in your DataMapper model.  You do this by adding a class variable of <kbd>$table</kbd>, which should be the name of your table. For example:</p>

<code>
class <var>Country</var> extends <samp>DataMapper</samp> {<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;var <kbd>$table</kbd> = '<dfn>countries</dfn>';<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;function <var>Country</var>()<br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parent::<samp>DataMapper</samp>();<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
}<br />
<br />
/* End of file country.php */<br />
/* Location: ./application/models/country.php */
</code>

<p>If you don't supply the <kbd>$table</kbd> variable, DataMapper will automatically assume the table name is the same as your model name, in lowercase, with the letter <b>s</b> on the end (which will be the case most of the time).</p>
<p>However, with that said, I have included a customised version of CodeIgniter's <strong>Inflector Helper</strong> with DataMapper that should be able to correctly convert most irregular singular/plural words, if loaded.</p>

<p>There is one other scenario to look at where the singular and plural name of an object can get a little confusing.  What do you do if the singular name of an object is the same as the plural name?  For example, the word <b>fruit</b> is used for both a single piece of fruit and multiple pieces of fruit.  In this case, you will have to use the singular model name of <b>Fruit</b> and the plural table name of <b>fruits</b>.  Alternatively, you can specify a different table name to the automatically determined name, in the same way as done above.</p>


</div>
<!-- END CONTENT -->


<div id="footer">
<p>
Previous Topic:&nbsp;&nbsp;<a href="database.html">Database Tables</a>
&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
Next Topic:&nbsp;&nbsp;<a href="prefix.html">Setting up Table Prefixes</a>
</p>
<p><a href="http://stensi.com">DataMapper</a> &nbsp;&middot;&nbsp; Copyright &#169; 2008 &nbsp;&middot;&nbsp; <a href="http://stensi.com/">Simon Stenhouse</a></p>
<p><a href="http://codeigniter.com">CodeIgniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2006-2008 &nbsp;&middot;&nbsp; <a href="http://ellislab.com/">Ellislab, Inc.</a></p>
</div>

<script type="text/javascript" src="../js/mootools.js"></script>
<script type="text/javascript" src="../js/menu.js"></script>
<script type="text/javascript">
<!--
	window.addEvent('domready', function() {

		// Create Menu
		var menu = new Menu({
			basepath: '../',
			pagepath: ''
		});

	});	
//-->
</script>
</body>
</html>